﻿using System;
using System.Text;
using System.Net;
using System.Collections.Generic;
using System.Web.UI.MobileControls;
using System.Configuration;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Tags;
using Winista.Text.HtmlParser.Filters;

namespace WSOAMOBIMIDWARE
{
    public class WSOAService
    {
        //使用的根目录
        public static string _HTTPROOTURL = string.Empty;
        public static string HTTPROOTURL
        {
            get
            {
                //if (string.IsNullOrEmpty(_HTTPROOTURL))
                //{
                //    lock (_HTTPROOTURL)
                //    {
                //        SystemConfig config = AppUtil.DeSerializeXML<SystemConfig>(AppDomain.CurrentDomain.BaseDirectory + "\\systemconfig.xml");
                //        if (config != null)
                //        {
                //            _HTTPROOTURL = config.rooturl;
                //        }
                //        else
                //        {
                //            _HTTPROOTURL = "http://oa.well-soft.com:8081";
                //            config = new SystemConfig();
                //            config.rooturl = "http://oa.well-soft.com:8081";
                //            AppUtil.SerializeXML<SystemConfig>(config, AppDomain.CurrentDomain.BaseDirectory + "\\systemconfig.xml");
                //        }
                //    }
                //}
                //return _HTTPROOTURL;
                //return "http://192.168.0.249:8081";
                return ConfigurationManager.AppSettings["HTTPROOTURL"];
                //return "http://oa.well-soft.com:9005";
            }
            set
            {
                _HTTPROOTURL = value;
            }
        }
        //public static string HTTPROOTURL = "http://oa.well-soft.com:8081";
        //android推送服务端
        public static string ANDROIDPNSERVERURL = "127.0.0.1:7070";
        //在线用户设备
        public static Dictionary<string, OnlineUser> onLineUsers = null;
        //登陆地址
        public const string LOGINURL = "/names.nsf?Login";
        //登陆后跳转地址
        public const string LOGINREDIRECTURL = "/servlet/PDALogon?open&username={0}&password={1}&client=html";
        //POST方法
        public const string HTTPPOSTMETHOD = "POST";
        //GET方法
        public const string HTTPGETMETHOD = "GET";

        public static Dictionary<string, OnlineUser> getOnLineUser()
        {
            //if (onLineUsers == null)
            //{
            //    onLineUsers = new Dictionary<string, OnlineUser>();
            //    lock (onLineUsers)
            //    {
            //        List<OnlineUser> users = AppUtil.DeSerializeXML<List<OnlineUser>>(AppDomain.CurrentDomain.BaseDirectory + "\\onlineusers.xml");
            //        if (users != null)
            //        {
            //            for (int i = 0; i < users.Count; i++)
            //            {
            //                onLineUsers.Add(users[i].userName.ToUpper(), users[i]);
            //            }
            //        }
            //    }
            //}
            //return onLineUsers;
            if (onLineUsers == null)
            {
                onLineUsers = new Dictionary<string, OnlineUser>();
            }
            return onLineUsers;
        }

        public static void setOnLineUser(string username, OnlineUser user)
        {
            if (onLineUsers == null)
            {
                onLineUsers = new Dictionary<string, OnlineUser>();
            }
            if (onLineUsers.ContainsKey(username.ToUpper()))
            {
                onLineUsers[username.ToUpper()] = user;
            }
            else
            {
                onLineUsers.Add(username.ToUpper(), user);
            }
            //if (onLineUsers == null)
            //{
            //    onLineUsers = new Dictionary<string, OnlineUser>();
            //    lock (onLineUsers)
            //    {
            //        List<OnlineUser> users = AppUtil.DeSerializeXML<List<OnlineUser>>(AppDomain.CurrentDomain.BaseDirectory + "\\onlineusers.xml");
            //        if (users != null)
            //        {
            //            for (int i = 0; i < users.Count; i++)
            //            {
            //                onLineUsers.Add(users[i].userName.ToUpper(), users[i]);
            //            }
            //        }
            //        if (onLineUsers.ContainsKey(username.ToUpper()))
            //        {
            //            onLineUsers[username.ToUpper()] = user;
            //        }
            //        else
            //        {
            //            onLineUsers.Add(username.ToUpper(), user);
            //        }
            //        List<OnlineUser> setUsers = new List<OnlineUser>();
            //        foreach (string key in onLineUsers.Keys)
            //        {
            //            setUsers.Add(onLineUsers[key]);
            //        }
            //        AppUtil.SerializeXML<List<OnlineUser>>(setUsers, AppDomain.CurrentDomain.BaseDirectory + "\\onlineusers.xml");
            //    }
            //}
            //else
            //{
            //    lock (onLineUsers)
            //    {
            //        if (onLineUsers.ContainsKey(username.ToUpper()))
            //        {
            //            onLineUsers[username.ToUpper()] = user;
            //        }
            //        else
            //        {
            //            onLineUsers.Add(username.ToUpper(), user);
            //        }
            //        List<OnlineUser> setUsers = new List<OnlineUser>();
            //        foreach (string key in onLineUsers.Keys)
            //        {
            //            setUsers.Add(onLineUsers[key]);
            //        }
            //        AppUtil.SerializeXML<List<OnlineUser>>(setUsers, AppDomain.CurrentDomain.BaseDirectory + "\\onlineusers.xml");
            //    }
            //}
        }

        public static WSOA_Login_Response helloworld(string str1, string str2)
        {
            ////添加用户及设备
            //OnlineUser user = new OnlineUser();
            //user.userName = name;
            //user.deviceType = "IOS";
            //user.deviceToken = "ABC";
            //setOnLineUser(name.ToUpper(), user);
            //Console.WriteLine(onLineUsers.ToString());
            WSOA_Login_Request request = new WSOA_Login_Request();
            request.username = str1;
            request.password = str2;
            request.deviceType = "AND";
            return Login(request);
            //return ConfigurationManager.AppSettings["addkey1"];
        }

        /// <summary>
        /// 登陆系统
        /// </summary>
        public static WSOA_Login_Response Login(WSOA_Login_Request request) 
        {
            //使用的根目录
            //string HTTPROOTURL = ConfigurationManager.AppSettings["HTTPROOTURL"];

            WSOA_Login_Response serviceResponse = new WSOA_Login_Response();
            HTTPOA_Login_Response login_Response = null;
            List<WSOA_Cookie> cookieList = new List<WSOA_Cookie>();

            try
            {
                ////发送第一次请求，并获取Cookie
                //HttpResponse response1 = HttpService.HttpRequest(HTTPROOTURL + LOGINURL, HTTPPOSTMETHOD, string.Format("Username={0}&Password={1}", request.username, request.password), Encoding.UTF8, null);
                //if (!response1.exeFlag)
                //{
                //    serviceResponse.exeFlag = 0;
                //    serviceResponse.message = response1.message;
                //    return serviceResponse;
                //}
                //if (string.IsNullOrEmpty(response1.setCookie))
                //{
                //    serviceResponse.exeFlag = 0;
                //    serviceResponse.message = "用户名或密码错误！";
                //    return serviceResponse;
                //}
                //string[] cookies = response1.setCookie.Split(';');
                //string[] cookieSessionId = cookies[0].Split('=');
                //string[] cookiePath = cookies[1].Split('=');

                HttpResponse response2 = HttpService.HttpRequest(HTTPROOTURL + string.Format(LOGINREDIRECTURL, request.username, request.password), HTTPGETMETHOD, null, Encoding.UTF8, null);
                if (!response2.exeFlag)
                {
                    serviceResponse.exeFlag = 0;
                    serviceResponse.message = response2.message;
                    return serviceResponse;
                }
                //序列化返回数据
                XmlService.DeSerializeFromXML<HTTPOA_Login_Response>(out login_Response, response2.returnStr);

                serviceResponse = new WSOA_Login_Response();
                //判断是否序列化成功
                if (login_Response == null)
                {
                    serviceResponse.exeFlag = 0;
                    serviceResponse.message = "返回数据异常！";
                    return serviceResponse;
                }

                //服务器处理是否成功
                if (!login_Response.status.Equals("success"))
                {
                    serviceResponse.exeFlag = 0;
                    serviceResponse.message = login_Response.prompt.text;
                    return serviceResponse;
                }

                WSOA_Cookie cookie = new WSOA_Cookie();
                cookie.name = "COOKIE";
                cookie.value = login_Response.session + ";" + login_Response.cookie;
                cookieList.Add(cookie);

                //建立返回对象
                serviceResponse.exeFlag = 1;
                serviceResponse.cookies = cookieList.ToArray();
                serviceResponse.clientUpdate = new WSOA_ClientUpdate();
                serviceResponse.clientUpdate.url = login_Response.update.url;
                serviceResponse.clientUpdate.ver = login_Response.update.ver;

                if (login_Response.downloads != null && login_Response.downloads.Length > 0)
                {
                    serviceResponse.downloadFileInfo = new WSOA_DownloadFileInfo[login_Response.downloads.Length];
                    for (int i = 0; i < login_Response.downloads.Length; i++)
                    {
                        serviceResponse.downloadFileInfo[i] = new WSOA_DownloadFileInfo();
                        serviceResponse.downloadFileInfo[i].fileId = login_Response.downloads[i].id;
                        serviceResponse.downloadFileInfo[i].url = login_Response.downloads[i].url;
                        serviceResponse.downloadFileInfo[i].ver = login_Response.downloads[i].ver;
                    }
                }
                serviceResponse.nextUrl = login_Response.next.url;
                serviceResponse.pageProgress = new WSOA_PageProgress();
                serviceResponse.pageProgress.message = login_Response.next.process.message;
                serviceResponse.pageProgress.speed = login_Response.next.process.speed;

                if (!string.IsNullOrEmpty(request.isRegisterDevice) && request.isRegisterDevice.Equals("1"))
                {
                    //添加用户及设备
                    OnlineUser user = new OnlineUser();
                    user.userName = request.username;
                    user.deviceType = request.deviceType;
                    user.deviceToken = request.deviceToken;
                    setOnLineUser(request.username.ToUpper(), user);
                }

                return serviceResponse;
            }
            catch (Exception e)
            {
                serviceResponse.exeFlag = 0;
                serviceResponse.message = e.Message;
                return serviceResponse;
            }
        }

        /// <summary>
        /// 推送消息
        /// </summary>
        public static WSOA_PushNotification_Response WSOA_PushNotification(WSOA_PushNotification_Request request)
        {
            WSOA_PushNotification_Response response = new WSOA_PushNotification_Response();
            StringBuilder errorSb = new StringBuilder();
            if (request == null || string.IsNullOrEmpty(request.receiver))
            {
                response.result = "0";
                response.message = "没有收件人信息！";
            }
            if (string.IsNullOrEmpty(request.content) || string.IsNullOrEmpty(request.url))
            {
                response.result = "0";
                response.message = "错误的信息！";
            }
            string[] receivers = request.receiver.Split(';');
            List<PNMessage> iosMsgs = new List<PNMessage>();
            List<PNMessage> andMsgs = new List<PNMessage>();
            for (int i = 0; i < receivers.Length; i++)
            {
                if (getOnLineUser().ContainsKey(receivers[i].ToUpper()))
                {
                    OnlineUser user = getOnLineUser()[receivers[i].ToUpper()];

                    PNMessage msg = new PNMessage();
                    msg.username = user.userName;
                    msg.receiverDeviceToken = user.deviceToken;
                    msg.content = request.content;
                    msg.url = request.url;

                    if (user.deviceType.Equals("IOS"))
                    {
                        iosMsgs.Add(msg);
                    }
                    else if (user.deviceType.Equals("AND"))
                    {
                        andMsgs.Add(msg);
                    }
                    else
                    {
                        errorSb.Append(receivers[i]);
                        errorSb.Append(": ");
                        errorSb.Append("用户注册了不支持的设备信息！;");
                    }
                }
                else
                {
                    errorSb.Append(receivers[i]);
                    errorSb.Append(": ");
                    errorSb.Append("用户未注册设备信息！;");
                }
            }
            string iosErrorMsg = PushNotificationService.pushIOSNotifications(iosMsgs);
            errorSb.Append(iosErrorMsg);
            string androidErrorMsg = PushNotificationService.pushAndroidNotifications(ANDROIDPNSERVERURL, andMsgs);
            errorSb.Append(androidErrorMsg);
            if (!string.IsNullOrEmpty(errorSb.ToString().Replace("{}", "")))
            {
                response.result = "0";
                response.message = errorSb.ToString();
            }
            else
            {
                response.result = "1";
            }
            return response;
        }

        /// <summary>
        /// 使用Get方式提交服务器
        /// </summary>
        public static WSOA_HttpGet_Response HttpGet(WSOA_HttpGet_Request request)
        {
            //使用的根目录
            //string HTTPROOTURL = ConfigurationManager.AppSettings["HTTPROOTURL"];

            WSOA_HttpGet_Response serviceResponse = new WSOA_HttpGet_Response();
            HttpResponse response = null;
            HTTPOA_Page page = null;
            string url = string.Empty;

            try
            {
                if (request.url.ToUpper().StartsWith("HTTP://") || request.url.ToUpper().StartsWith("HTTPS://"))
                {
                    url = request.url.Replace("amp;", "");
                }
                else
                {
                    url = HTTPROOTURL + request.url.Replace("amp;", "");
                }
                response = HttpService.HttpRequest(url, HTTPPOSTMETHOD, null, Encoding.UTF8, request.cookies);
                if (!response.exeFlag)
                {
                    serviceResponse.exeFlag = 0;
                    serviceResponse.message = response.message;
                    return serviceResponse;
                }
                if (response.returnStr.IndexOf("ID_LoginFormMark") > -1)
                {
                    serviceResponse.exeFlag = 2;
                    serviceResponse.message = "登陆已超时！";
                    return serviceResponse;
                }

                //序列化返回数据
                XmlService.DeSerializeFromXML<HTTPOA_Page>(out page, response.returnStr);
                //判断是否序列化成功
                if (page == null)
                {
                    serviceResponse.exeFlag = 0;
                    serviceResponse.message = "返回数据异常！";
                    return serviceResponse;
                }
                if (!string.IsNullOrEmpty(page.status) && page.status.ToUpper().Equals("ERROR"))
                {
                    serviceResponse.exeFlag = 0;
                    if (page.prompt != null && !string.IsNullOrEmpty(page.prompt.message))
                    {
                        serviceResponse.message = page.prompt.message;
                    }
                    else
                    {
                        serviceResponse.message = "返回数据异常！";
                    }
                    return serviceResponse;
                }
                serviceResponse.exeFlag = 1;
                serviceResponse.nextPage = ConvertPageObject(page, request.cookies);
                return serviceResponse;
            }
            catch (Exception e)
            {
                serviceResponse.exeFlag = 0;
                serviceResponse.message = e.Message;
                return serviceResponse;
            }
        }

        /// <summary>
        /// 通讯录处理
        /// </summary>
        public static WSOA_AddressProcess_Response AddressProcess(WSOA_AddressProcess_Request request)
        {
            //使用的根目录
            //string HTTPROOTURL = ConfigurationManager.AppSettings["HTTPROOTURL"];

            string downloadUrl = string.Empty;
            string uploadUrl = string.Empty;

            if (request.downloadUrl.ToUpper().StartsWith("HTTP"))
            {
                downloadUrl = request.downloadUrl.Replace("amp;", "");
            }
            else
            {
                downloadUrl = HTTPROOTURL + request.downloadUrl.Replace("amp;", "");
            }

            if (request.uploadUrl.ToUpper().StartsWith("HTTP"))
            {
                uploadUrl = request.uploadUrl.Replace("amp;", "");
            }
            else
            {
                uploadUrl = HTTPROOTURL + request.uploadUrl.Replace("amp;", "");
            }

            HTTPOA_AddressBook addressBookRequest = null;

            string requestXml = string.Empty;

            WSOA_AddressProcess_Response serviceResponse = new WSOA_AddressProcess_Response();
            HttpResponse response = null;
            HTTPOA_AddressBook addressBook = null;

            if (request.processType.Equals("up") || request.processType.Equals("updown"))
            {
                addressBookRequest = new HTTPOA_AddressBook();
                addressBookRequest.xt = "address";
                addressBookRequest.direct = request.processType;
                addressBookRequest.user = request.user;

                if (request.address != null && request.address.Length > 0)
                {
                    addressBookRequest.address = new HTTPOA_Address[request.address.Length];
                    for (int i = 0; i < request.address.Length; i++)
                    {
                        addressBookRequest.address[i] = new HTTPOA_Address();
                        if (request.address[i].fields != null && request.address[i].fields.Length > 0)
                        {
                            addressBookRequest.address[i].fields = new HTTPOA_PostField[request.address[i].fields.Length];
                            for (int j = 0; j < request.address[i].fields.Length; j++)
                            {
                                addressBookRequest.address[i].fields[j] = new HTTPOA_PostField();
                                addressBookRequest.address[i].fields[j].name = request.address[i].fields[j].fieldName;
                                addressBookRequest.address[i].fields[j].values = request.address[i].fields[j].fieldValues;
                                if (addressBookRequest.address[i].fields[j].values != null && addressBookRequest.address[i].fields[j].values.Length > 0)
                                {
                                    for (int k = 0; k < addressBookRequest.address[i].fields[j].values.Length; k++)
                                    {
                                        addressBookRequest.address[i].fields[j].values[k] = "<![CDATA[" + escape(addressBookRequest.address[i].fields[j].values[k].Replace("amp;", "").Replace("&gt;", ">").Replace("&lt;", "<")) + "]]>";
                                    }
                                }
                            }
                        }
                    }
                }

                requestXml = XmlService.SerializeToXML<HTTPOA_AddressBook>(addressBookRequest);
                requestXml = requestXml.Replace("&gt;", ">").Replace("&lt;", "<");

                response = HttpService.HttpRequest(uploadUrl, HTTPPOSTMETHOD, requestXml, Encoding.UTF8, request.cookies);
                if (!response.exeFlag)
                {
                    serviceResponse.exeFlag = 0;
                    serviceResponse.message = response.message;
                    return serviceResponse;
                }
                if (response.returnStr.IndexOf("ID_LoginFormMark") > -1)
                {
                    serviceResponse.exeFlag = 2;
                    serviceResponse.message = "登陆已超时！";
                    return serviceResponse;
                }
            }

            if (request.processType.Equals("down") || request.processType.Equals("updown"))
            {
                response = HttpService.HttpRequest(downloadUrl + "&Last=" + request.updateTime, HTTPPOSTMETHOD, null, Encoding.UTF8, request.cookies);
                if (!response.exeFlag)
                {
                    serviceResponse.exeFlag = 0;
                    serviceResponse.message = response.message;
                    return serviceResponse;
                }

                //序列化返回数据
                XmlService.DeSerializeFromXML<HTTPOA_AddressBook>(out addressBook, response.returnStr);
                //判断是否序列化成功
                if (addressBook == null)
                {
                    serviceResponse.exeFlag = 0;
                    serviceResponse.message = "返回数据异常！";
                    return serviceResponse;
                }
                if (response.returnStr.IndexOf("ID_LoginFormMark") > -1)
                {
                    serviceResponse.exeFlag = 2;
                    serviceResponse.message = "登陆已超时！";
                    return serviceResponse;
                }
                serviceResponse.exeFlag = 1;
                serviceResponse.updateTime = addressBook.updateTime;


                if (addressBook.address != null && addressBook.address.Length > 0)
                {
                    serviceResponse.address = new WSOA_Address[addressBook.address.Length];
                    for (int i = 0; i < addressBook.address.Length; i++)
                    {
                        if (addressBook.address[i].fields != null && addressBook.address[i].fields.Length > 0)
                        {
                            serviceResponse.address[i] = new WSOA_Address();
                            serviceResponse.address[i].fields = new WSOA_PostField[addressBook.address[i].fields.Length];
                            for (int j = 0; j < addressBook.address[i].fields.Length; j++)
                            {
                                serviceResponse.address[i].fields[j] = new WSOA_PostField();
                                serviceResponse.address[i].fields[j].fieldName = addressBook.address[i].fields[j].name;
                                serviceResponse.address[i].fields[j].fieldValues = addressBook.address[i].fields[j].values;
                            }
                        }
                    }
                }
            }

            serviceResponse.exeFlag = 1;
            return serviceResponse;
        }


        /// <summary>
        /// 日程表处理
        /// </summary>
        public static WSOA_AppointmentProcess_Response AppointmentProcess(WSOA_AppointmentProcess_Request request)
        {
            //使用的根目录
            //string HTTPROOTURL = ConfigurationManager.AppSettings["HTTPROOTURL"];

            string downloadUrl = string.Empty;
            string uploadUrl = string.Empty;

            if (request.downloadUrl.ToUpper().StartsWith("HTTP"))
            {
                downloadUrl = request.downloadUrl.Replace("amp;", "");
            }
            else
            {
                downloadUrl = HTTPROOTURL + request.downloadUrl.Replace("amp;", "");
            }

            if (request.uploadUrl.ToUpper().StartsWith("HTTP"))
            {
                uploadUrl = request.uploadUrl.Replace("amp;", "");
            }
            else
            {
                uploadUrl = HTTPROOTURL + request.uploadUrl.Replace("amp;", "");
            }

            HTTPOA_Schedule scheduleRequest = null;

            string requestXml = string.Empty;

            WSOA_AppointmentProcess_Response serviceResponse = new WSOA_AppointmentProcess_Response();
            HttpResponse response = null;
            HTTPOA_Schedule schedule = null;

            if (request.processType.Equals("up") || request.processType.Equals("updown"))
            {
                scheduleRequest = new HTTPOA_Schedule();
                scheduleRequest.xt = "appointment";
                scheduleRequest.direct = request.processType;
                scheduleRequest.user = request.user;

                if (request.appointment != null && request.appointment.Length > 0)
                {
                    scheduleRequest.appointment = new HTTPOA_Appointment[request.appointment.Length];
                    for (int i = 0; i < request.appointment.Length; i++)
                    {
                        scheduleRequest.appointment[i] = new HTTPOA_Appointment();
                        if (request.appointment[i].fields != null && request.appointment[i].fields.Length > 0)
                        {
                            scheduleRequest.appointment[i].fields = new HTTPOA_PostField[request.appointment[i].fields.Length];
                            for (int j = 0; j < request.appointment[i].fields.Length; j++)
                            {
                                scheduleRequest.appointment[i].fields[j] = new HTTPOA_PostField();
                                scheduleRequest.appointment[i].fields[j].name = request.appointment[i].fields[j].fieldName;
                                scheduleRequest.appointment[i].fields[j].values = request.appointment[i].fields[j].fieldValues;
                                if (scheduleRequest.appointment[i].fields[j].values != null && scheduleRequest.appointment[i].fields[j].values.Length > 0)
                                {
                                    for (int k = 0; k < scheduleRequest.appointment[i].fields[j].values.Length; k++)
                                    {
                                        scheduleRequest.appointment[i].fields[j].values[k] = "<![CDATA[" + escape(scheduleRequest.appointment[i].fields[j].values[k].Replace("amp;", "").Replace("&gt;", ">").Replace("&lt;", "<")) + "]]>";
                                    }
                                }
                            }
                        }
                    }
                }

                requestXml = XmlService.SerializeToXML<HTTPOA_Schedule>(scheduleRequest);
                requestXml = requestXml.Replace("&gt;", ">").Replace("&lt;", "<");

                response = HttpService.HttpRequest(uploadUrl, HTTPPOSTMETHOD, requestXml, Encoding.UTF8, request.cookies);
                if (!response.exeFlag)
                {
                    serviceResponse.exeFlag = 0;
                    serviceResponse.message = response.message;
                    return serviceResponse;
                }
                if (response.returnStr.IndexOf("ID_LoginFormMark") > -1)
                {
                    serviceResponse.exeFlag = 2;
                    serviceResponse.message = "登陆已超时！";
                    return serviceResponse;
                }
            }

            if (request.processType.Equals("down") || request.processType.Equals("updown"))
            {
                response = HttpService.HttpRequest(downloadUrl + "&Last=" + request.updateTime, HTTPPOSTMETHOD, null, Encoding.UTF8, request.cookies);
                if (!response.exeFlag)
                {
                    serviceResponse.exeFlag = 0;
                    serviceResponse.message = response.message;
                    return serviceResponse;
                }

                //序列化返回数据
                XmlService.DeSerializeFromXML<HTTPOA_Schedule>(out schedule, response.returnStr);
                //判断是否序列化成功
                if (schedule == null)
                {
                    serviceResponse.exeFlag = 0;
                    serviceResponse.message = "返回数据异常！";
                    return serviceResponse;
                }
                if (response.returnStr.IndexOf("ID_LoginFormMark") > -1)
                {
                    serviceResponse.exeFlag = 2;
                    serviceResponse.message = "登陆已超时！";
                    return serviceResponse;
                }
                serviceResponse.exeFlag = 1;
                serviceResponse.updateTime = schedule.updateTime;


                if (schedule.appointment != null && schedule.appointment.Length > 0)
                {
                    serviceResponse.appointment = new WSOA_Appointment[schedule.appointment.Length];
                    for (int i = 0; i < schedule.appointment.Length; i++)
                    {
                        if (schedule.appointment[i].fields != null && schedule.appointment[i].fields.Length > 0)
                        {
                            serviceResponse.appointment[i] = new WSOA_Appointment();
                            serviceResponse.appointment[i].fields = new WSOA_PostField[schedule.appointment[i].fields.Length];
                            for (int j = 0; j < schedule.appointment[i].fields.Length; j++)
                            {
                                serviceResponse.appointment[i].fields[j] = new WSOA_PostField();
                                serviceResponse.appointment[i].fields[j].fieldName = schedule.appointment[i].fields[j].name;
                                serviceResponse.appointment[i].fields[j].fieldValues = schedule.appointment[i].fields[j].values;
                            }
                        }
                    }
                }
            }

            serviceResponse.exeFlag = 1;
            return serviceResponse;
        }

        /// <summary>
        /// 使用Post方式提交服务器
        /// </summary>
        public static WSOA_HttpPost_Response HttpPost(WSOA_HttpPost_Request request)
        {
            //使用的根目录
            //string HTTPROOTURL = ConfigurationManager.AppSettings["HTTPROOTURL"];

            WSOA_HttpPost_Response serviceResponse = new WSOA_HttpPost_Response();
            HTTPOA_HttpPost_Request httpRequest = new HTTPOA_HttpPost_Request();
            string requestXml = string.Empty;
            HttpResponse response = null;
            HTTPOA_Page page = null;
            HTTPOA_HttpPost_Response httpResponse = null;

            string url = string.Empty;

            try
            {
                if (request.url.ToUpper().StartsWith("HTTP://") || request.url.ToUpper().StartsWith("HTTPS://"))
                {
                    url = request.url.Replace("amp;", "");
                }
                else
                {
                    url = HTTPROOTURL + request.url.Replace("amp;", "");
                }
                //构建请求
                httpRequest.xt = "submit";
                if (request.postFields != null && request.postFields.Length > 0)
                {
                    httpRequest.fields = new HTTPOA_PostField[request.postFields.Length];
                    for (int i = 0; i < request.postFields.Length; i++)
                    {
                        httpRequest.fields[i] = new HTTPOA_PostField();
                        httpRequest.fields[i].name = request.postFields[i].fieldName;
                        httpRequest.fields[i].values = request.postFields[i].fieldValues;
                        if (httpRequest.fields[i].values != null && httpRequest.fields[i].values.Length > 0)
                        {
                            for (int j = 0; j < httpRequest.fields[i].values.Length; j++)
                            {
                                httpRequest.fields[i].values[j] = "<![CDATA[" + escape(httpRequest.fields[i].values[j].Replace("amp;", "").Replace("&gt;", ">").Replace("&lt;", "<")) + "]]>";
                            }
                        }
                    }
                }
                requestXml = XmlService.SerializeToXML<HTTPOA_HttpPost_Request>(httpRequest);
                requestXml = requestXml.Replace("&gt;", ">").Replace("&lt;", "<");

                response = HttpService.HttpRequest(url, HTTPPOSTMETHOD, requestXml, Encoding.UTF8, request.cookies);
                if (!response.exeFlag)
                {
                    serviceResponse.exeFlag = 0;
                    serviceResponse.message = response.message;
                    return serviceResponse;
                }
                if (response.returnStr.IndexOf("ID_LoginFormMark") > -1)
                {
                    serviceResponse.exeFlag = 2;
                    serviceResponse.message = "登陆已超时！";
                    return serviceResponse;
                }
                if (response.returnStr.IndexOf("<xt>face</xt>") > -1)
                {
                    XmlService.DeSerializeFromXML<HTTPOA_Page>(out page, response.returnStr);
                    //判断是否序列化成功
                    if (page == null)
                    {
                        serviceResponse.exeFlag = 0;
                        serviceResponse.message = "返回数据异常！";
                        return serviceResponse;
                    }
                    serviceResponse.exeFlag = 1;
                    serviceResponse.nextPage = ConvertPageObject(page, request.cookies);
                }
                else
                {
                    //序列化返回数据
                    XmlService.DeSerializeFromXML<HTTPOA_HttpPost_Response>(out httpResponse, response.returnStr);
                    //判断是否序列化成功
                    if (httpResponse == null)
                    {
                        serviceResponse.exeFlag = 0;
                        serviceResponse.message = "返回数据异常！";
                        return serviceResponse;
                    }
                    if (!httpResponse.status.Equals("success"))
                    {
                        serviceResponse.exeFlag = 0;
                    }
                    else
                    {
                        serviceResponse.exeFlag = 1;
                    }
                    if (httpResponse.prompt != null)
                    {
                        serviceResponse.postRespPrompt = new WSOA_PagePrompt();
                        serviceResponse.postRespPrompt.img = httpResponse.prompt.img;
                        serviceResponse.postRespPrompt.message = httpResponse.prompt.message;
                        serviceResponse.postRespPrompt.type = httpResponse.prompt.type;
                    }
                    #region 解析按钮信息
                    if (httpResponse.pageFields != null && httpResponse.pageFields.Length > 0)
                    {
                        serviceResponse.pageFields = new WSOA_PageField[httpResponse.pageFields.Length];
                        for (int i = 0; i < httpResponse.pageFields.Length; i++)
                        {
                            serviceResponse.pageFields[i] = new WSOA_PageField();
                            serviceResponse.pageFields[i].fieldID = httpResponse.pageFields[i].fieldId;
                            serviceResponse.pageFields[i].fieldType = httpResponse.pageFields[i].type;
                            serviceResponse.pageFields[i].fieldTitle = httpResponse.pageFields[i].title;
                            serviceResponse.pageFields[i].fieldName = httpResponse.pageFields[i].name;
                            serviceResponse.pageFields[i].fieldValue = httpResponse.pageFields[i].value;
                            serviceResponse.pageFields[i].required = httpResponse.pageFields[i].require;
                            serviceResponse.pageFields[i].separator = httpResponse.pageFields[i].separator;
                            serviceResponse.pageFields[i].reset = httpResponse.pageFields[i].reset;
                            serviceResponse.pageFields[i].fieldHide = httpResponse.pageFields[i].hide;
                            serviceResponse.pageFields[i].fieldDisable = httpResponse.pageFields[i].dable;
                            serviceResponse.pageFields[i].fieldRead = httpResponse.pageFields[i].read;
                            serviceResponse.pageFields[i].rowCount = httpResponse.pageFields[i].rows;

                            if (httpResponse.pageFields[i].btn != null && httpResponse.pageFields[i].btn.Length > 0)
                            {
                                if (httpResponse.pageFields[i].type.Equals("button"))
                                {
                                    //不处理按钮控件
                                }
                                else
                                {
                                    serviceResponse.pageFields[i].fieldButtons = new WSOA_PageFieldButton[httpResponse.pageFields[i].btn.Length];
                                    for (int j = 0; j < httpResponse.pageFields[i].btn.Length; j++)
                                    {
                                        serviceResponse.pageFields[i].fieldButtons[j] = new WSOA_PageFieldButton();
                                        serviceResponse.pageFields[i].fieldButtons[j].actionID = httpResponse.pageFields[i].btn[j].actionId;
                                        serviceResponse.pageFields[i].fieldButtons[j].img = httpResponse.pageFields[i].btn[j].img;
                                        serviceResponse.pageFields[i].fieldButtons[j].message = httpResponse.pageFields[i].btn[j].message;
                                        serviceResponse.pageFields[i].fieldButtons[j].action = ConvertActionObject(httpResponse.pageFields[i].btn[j].action);
                                    }
                                }
                            }

                            if (httpResponse.pageFields[i].option != null && httpResponse.pageFields[i].option.Length > 0)
                            {
                                serviceResponse.pageFields[i].fieldOptions = new WSOA_PageFieldOption[httpResponse.pageFields[i].option.Length];
                                for (int j = 0; j < httpResponse.pageFields[i].option.Length; j++)
                                {
                                    serviceResponse.pageFields[i].fieldOptions[j] = new WSOA_PageFieldOption();
                                    serviceResponse.pageFields[i].fieldOptions[j].optionID = httpResponse.pageFields[i].option[j].optionId;
                                    serviceResponse.pageFields[i].fieldOptions[j].message = httpResponse.pageFields[i].option[j].message;
                                    serviceResponse.pageFields[i].fieldOptions[j].optionValue = httpResponse.pageFields[i].option[j].value;
                                    serviceResponse.pageFields[i].fieldOptions[j].optionHide = httpResponse.pageFields[i].option[j].hide;
                                    serviceResponse.pageFields[i].fieldOptions[j].optionDisable = httpResponse.pageFields[i].option[j].dable;
                                    serviceResponse.pageFields[i].fieldOptions[j].optionRead = httpResponse.pageFields[i].option[j].optionId;
                                    if (httpResponse.pageFields[i].option[j].saction != null)
                                    {
                                        serviceResponse.pageFields[i].fieldOptions[j].sactionShow = httpResponse.pageFields[i].option[j].saction.show;
                                        serviceResponse.pageFields[i].fieldOptions[j].sactionHide = httpResponse.pageFields[i].option[j].saction.hide;
                                        serviceResponse.pageFields[i].fieldOptions[j].sactionAble = httpResponse.pageFields[i].option[j].saction.able;
                                        serviceResponse.pageFields[i].fieldOptions[j].sactionDisable = httpResponse.pageFields[i].option[j].saction.dable;
                                    }
                                    if (httpResponse.pageFields[i].option[j].saction != null)
                                    {
                                        serviceResponse.pageFields[i].fieldOptions[j].usactionShow = httpResponse.pageFields[i].option[j].usaction.show;
                                        serviceResponse.pageFields[i].fieldOptions[j].usactionHide = httpResponse.pageFields[i].option[j].usaction.hide;
                                        serviceResponse.pageFields[i].fieldOptions[j].usactionAble = httpResponse.pageFields[i].option[j].usaction.able;
                                        serviceResponse.pageFields[i].fieldOptions[j].usactionDisable = httpResponse.pageFields[i].option[j].usaction.dable;
                                    }
                                }
                            }
                        }
                    }
                    #endregion
                    if (httpResponse.actions != null && httpResponse.actions.Length > 0)
                    {
                        serviceResponse.actions = new WSOA_Action[httpResponse.actions.Length];
                        for (int i = 0; i < httpResponse.actions.Length; i++)
                        {
                            serviceResponse.actions[i] = ConvertActionObject(httpResponse.actions[i]);
                        }
                    }
                    serviceResponse.nextActionID = httpResponse.nextActionId;
                    serviceResponse.nextAction = ConvertActionObject(httpResponse.nextAction);
                }
                return serviceResponse;
            }
            catch (Exception e)
            {
                serviceResponse.exeFlag = 0;
                serviceResponse.message = e.Message;
                return serviceResponse;
            }
        }

        /// <summary>
        /// 使用Get方式下载组织信息
        /// </summary>
        public static WSOA_DownloadUnit_Response DownloadUnit(WSOA_DownloadUnit_Request request)
        {
            //使用的根目录
            //string HTTPROOTURL = ConfigurationManager.AppSettings["HTTPROOTURL"];

            string url = string.Empty;

            if (request.url.ToUpper().StartsWith("HTTP://") || request.url.ToUpper().StartsWith("HTTPS://"))
            {
                url = request.url.Replace("amp;", "");
            }
            else
            {
                url = HTTPROOTURL + request.url.Replace("amp;", "");
            }

            WSOA_DownloadUnit_Response serviceResponse = new WSOA_DownloadUnit_Response();
            HttpResponse response = null;
            HTTPOA_DownloadUnit_Response unit = null;

            response = HttpService.HttpRequest(url, HTTPPOSTMETHOD, null, Encoding.UTF8, request.cookies);
            if (!response.exeFlag)
            {
                serviceResponse.exeFlag = 0;
                serviceResponse.message = response.message;
                return serviceResponse;
            }
            if (response.returnStr.IndexOf("ID_LoginFormMark") > -1)
            {
                serviceResponse.exeFlag = 2;
                serviceResponse.message = "登陆已超时！";
                return serviceResponse;
            }

            //序列化返回数据
            XmlService.DeSerializeFromXML<HTTPOA_DownloadUnit_Response>(out unit, response.returnStr);
            //判断是否序列化成功
            if (unit == null)
            {
                serviceResponse.exeFlag = 0;
                serviceResponse.message = "返回数据异常！";
                return serviceResponse;
            }
            serviceResponse.exeFlag = 1;
            serviceResponse.depts = ConvertDeptObject(unit.depts);
            return serviceResponse;
        }


        /// <summary>
        /// 使用Get方式下载意见信息
        /// </summary>
        public static WSOA_DownloadOpinion_Response DownloadOpinion(WSOA_DownloadOpinion_Request request)
        {
            //使用的根目录
            //string HTTPROOTURL = ConfigurationManager.AppSettings["HTTPROOTURL"];

            string url = string.Empty;

            if (request.url.ToUpper().StartsWith("HTTP://") || request.url.ToUpper().StartsWith("HTTPS://"))
            {
                url = request.url.Replace("amp;", "");
            }
            else
            {
                url = HTTPROOTURL + request.url.Replace("amp;", "");
            }

            WSOA_DownloadOpinion_Response serviceResponse = new WSOA_DownloadOpinion_Response();
            HttpResponse response = null;
            HTTPOA_DownloadOpinion_Response opinion = null;

            response = HttpService.HttpRequest(url, HTTPPOSTMETHOD, null, Encoding.UTF8, request.cookies);
            if (!response.exeFlag)
            {
                serviceResponse.exeFlag = 0;
                serviceResponse.message = response.message;
                return serviceResponse;
            }
            if (response.returnStr.IndexOf("ID_LoginFormMark") > -1)
            {
                serviceResponse.exeFlag = 2;
                serviceResponse.message = "登陆已超时！";
                return serviceResponse;
            }

            //序列化返回数据
            XmlService.DeSerializeFromXML<HTTPOA_DownloadOpinion_Response>(out opinion, response.returnStr);
            //判断是否序列化成功
            if (opinion == null)
            {
                serviceResponse.exeFlag = 0;
                serviceResponse.message = "返回数据异常！";
                return serviceResponse;
            }
            serviceResponse.exeFlag = 1;
            serviceResponse.opinions = opinion.opinions;
            return serviceResponse;
        }

        /// <summary>
        /// 使用Get方式下载组织信息
        /// </summary>
        public static WSOA_DownloadFile_Response DownloadFile(WSOA_DownloadFile_Request request)
        {
            //使用的根目录
            //string HTTPROOTURL = ConfigurationManager.AppSettings["HTTPROOTURL"];

            string url = string.Empty;

            if (request.url.ToUpper().StartsWith("HTTP://") || request.url.ToUpper().StartsWith("HTTPS://"))
            {
                url = request.url.Replace("amp;", "");
            }
            else
            {
                url = HTTPROOTURL + request.url.Replace("amp;", "");
            }

            WSOA_DownloadFile_Response serviceResponse = new WSOA_DownloadFile_Response();
            HttpResponse response = null;

            response = HttpService.HttpRequestFile(url, HTTPPOSTMETHOD, null, Encoding.UTF8, request.cookies);
            if (!response.exeFlag)
            {
                if (response.message.Equals("timeout"))
                {
                    serviceResponse.exeFlag = 2;
                    serviceResponse.message = "登陆已超时！";
                }
                else
                {
                    serviceResponse.exeFlag = 0;
                    serviceResponse.message = response.message;
                }
                return serviceResponse;
            }

            serviceResponse.exeFlag = 1;
            serviceResponse.fileContent = (byte[])response.returnObj;
            return serviceResponse;
        }

        /// <summary>
        /// 转换页面对象
        /// </summary>
        public static WSOA_Page ConvertPageObject(HTTPOA_Page page, WSOA_Cookie[] cookies)
        {
            if (page == null)
            {
                return null;
            }
            WSOA_Page returnPage = new WSOA_Page();
            List<WSOA_PageMenu> menus = new List<WSOA_PageMenu>();

            returnPage.pageType = page.type;
            returnPage.markMsgCount = page.markMsgCount;
            returnPage.minusMarkMsgCount = page.minusMarkMsgCount;
            if (page.title != null)
            {
                returnPage.pageTitle = new WSOA_PageTitle();
                returnPage.pageTitle.img = page.title.img;
                if (page.stbar != null && !string.IsNullOrEmpty(page.stbar.message))
                {
                    returnPage.pageTitle.message = page.stbar.message;
                }
                else
                {
                    returnPage.pageTitle.message = page.title.message;
                }
                returnPage.pageTitle.desc = page.title.desc;
            }
            if (page.menus != null && page.menus.Length > 0)
            {
                #region 解析按钮信息
                returnPage.pageListSet = new WSOA_PageListSet();
                for (int i = 0; i < page.menus.Length; i++)
                {
                    if ((page.menus[i].isHidden != null && page.menus[i].isHidden.Equals("0")) || (page.menus[i].menuSetId != null && page.menus[i].menuSetId.ToUpper().Equals("LEFTUP")) || (page.menus[i].menuSetId != null && page.menus[i].menuSetId.ToUpper().Equals("RIGHTUP")))
                    {
                        continue;
                    }
                    if (page.menus[i].isDisabled != null && page.menus[i].isDisabled.Equals("1"))
                    {
                        continue;
                    }
                    if (!string.IsNullOrEmpty(page.menus[i].actionId) || page.menus[i].action != null)
                    {
                        if (isMenuScreened(page.menus[i].message)) continue;
                        WSOA_PageMenu pageMenu = new WSOA_PageMenu();
                        pageMenu.menuID = page.menus[i].menuSetId;
                        pageMenu.img = page.menus[i].img;
                        pageMenu.message = page.menus[i].message;
                        pageMenu.actionID = page.menus[i].actionId;
                        pageMenu.action = ConvertActionObject(page.menus[i].action);
                        pageMenu.isHidden = page.menus[i].isHidden;

                        menus.Add(pageMenu);

                        continue;
                    }
                    if (page.menus[i].menu == null || page.menus[i].menu.Length == 0)
                    {
                        continue;
                    }
                    for (int j = 0; j < page.menus[i].menu.Length; j++)
                    {
                        //如果按钮是分割线，不发送
                        if (page.menus[i].menu[j].type == "1")
                        {
                            continue;
                        }
                        if (!string.IsNullOrEmpty(page.menus[i].menu[j].actionId) || page.menus[i].menu[j].action != null)
                        {
                            //if (isMenuScreened(page.menus[i].menu[j].message)) continue;

                            if (page.menus[i].menu[j].isDisabled != null && page.menus[i].menu[j].isDisabled.Equals("1"))
                            {
                                continue;
                            }

                            WSOA_PageMenu pageMenu = new WSOA_PageMenu();
                            pageMenu.menuID = page.menus[i].menu[j].menuId;
                            pageMenu.img = page.menus[i].menu[j].img;
                            pageMenu.message = page.menus[i].menu[j].message;
                            pageMenu.actionID = page.menus[i].menu[j].actionId;
                            pageMenu.action = ConvertActionObject(page.menus[i].menu[j].action);
                            pageMenu.isHidden = page.menus[i].menu[j].isHidden;

                            menus.Add(pageMenu);
                            continue;
                        }
                        if (page.menus[i].menu[j].menu == null || page.menus[i].menu[j].menu.Length == 0)
                        {
                            continue;
                        }
                        for (int k = 0; k < page.menus[i].menu[j].menu.Length; k++)
                        {
                            //如果按钮是分割线，不发送
                            if (page.menus[i].menu[j].menu[k].type == "1")
                            {
                                continue;
                            }
                            if (!string.IsNullOrEmpty(page.menus[i].menu[j].menu[k].actionId) || page.menus[i].menu[j].menu[k].action != null)
                            {
                                //if (isMenuScreened(page.menus[i].menu[j].message + "-" + page.menus[i].menu[j].menu[k].message)) continue;

                                if (page.menus[i].menu[j].menu[k].isDisabled != null && page.menus[i].menu[j].menu[k].isDisabled.Equals("1"))
                                {
                                    continue;
                                }
                                WSOA_PageMenu pageMenu = new WSOA_PageMenu();
                                pageMenu.menuID = page.menus[i].menu[j].menu[k].menuId;
                                pageMenu.img = page.menus[i].menu[j].menu[k].img;
                                pageMenu.message = page.menus[i].menu[j].message + "-" + page.menus[i].menu[j].menu[k].message;
                                pageMenu.actionID = page.menus[i].menu[j].menu[k].actionId;
                                pageMenu.action = ConvertActionObject(page.menus[i].menu[j].menu[k].action);
                                pageMenu.isHidden = page.menus[i].menu[j].menu[k].isHidden;

                                menus.Add(pageMenu);
                            }
                        }
                    }
                }
                #endregion
            }
            if (page.lists != null)
            {
                #region 解析列表信息
                returnPage.pageListSet = new WSOA_PageListSet();
                returnPage.pageListSet.listType = page.lists.type;
                if (page.lists.list != null && page.lists.list.Length > 0)
                {
                    returnPage.pageListSet.listItems = new WSOA_PageListItem[page.lists.list.Length];
                    for (int i = 0; i < page.lists.list.Length;i++ )
                    {
                        returnPage.pageListSet.listItems[i] = new WSOA_PageListItem();
                        returnPage.pageListSet.listItems[i].img = page.lists.list[i].img;
                        returnPage.pageListSet.listItems[i].message = page.lists.list[i].message;
                        returnPage.pageListSet.listItems[i].count = page.lists.list[i].count;
                        returnPage.pageListSet.listItems[i].tail = page.lists.list[i].tail;
                        returnPage.pageListSet.listItems[i].value = page.lists.list[i].value;
                        returnPage.pageListSet.listItems[i].hiddenValue = page.lists.list[i].hvalue;
                        if (page.lists.list[i].tel != null)
                        {
                            returnPage.pageListSet.listItems[i].userInfo = new WSOA_UserInfo();
                            returnPage.pageListSet.listItems[i].userInfo.userName = page.lists.list[i].tel.name;
                            if (page.lists.list[i].tel.phones != null)
                            {
                                returnPage.pageListSet.listItems[i].userInfo.phoneInfo = new WSOA_PhoneInfo[page.lists.list[i].tel.phones.Length];
                                for (int j = 0; j < page.lists.list[i].tel.phones.Length; j++)
                                {
                                    returnPage.pageListSet.listItems[i].userInfo.phoneInfo[j] = new WSOA_PhoneInfo();
                                    returnPage.pageListSet.listItems[i].userInfo.phoneInfo[j].phoneNumber = page.lists.list[i].tel.phones[j].no;
                                    returnPage.pageListSet.listItems[i].userInfo.phoneInfo[j].phoneType = page.lists.list[i].tel.phones[j].type;
                                }
                            }
                        }
                        returnPage.pageListSet.listItems[i].img = page.lists.list[i].img;
                        returnPage.pageListSet.listItems[i].able = page.lists.list[i].able;
                        if (page.lists.list[i].listItems != null && page.lists.list[i].listItems.Length > 0)
                        {
                            returnPage.pageListSet.listItems[i].listSubItems = new WSOA_PageListSubItem[page.lists.list[i].listItems.Length];
                            for (int j = 0; j < page.lists.list[i].listItems.Length; j++)
                            {
                                returnPage.pageListSet.listItems[i].listSubItems[j] = new WSOA_PageListSubItem();

                                returnPage.pageListSet.listItems[i].listSubItems[j].img = page.lists.list[i].listItems[j].img;
                                returnPage.pageListSet.listItems[i].listSubItems[j].message = page.lists.list[i].listItems[j].message;
                                returnPage.pageListSet.listItems[i].listSubItems[j].tail = page.lists.list[i].listItems[j].tail;
                                returnPage.pageListSet.listItems[i].listSubItems[j].value = page.lists.list[i].listItems[j].value;
                                returnPage.pageListSet.listItems[i].listSubItems[j].hiddenValue = page.lists.list[i].listItems[j].hvalue;
                                returnPage.pageListSet.listItems[i].listSubItems[j].actionID = page.lists.list[i].listItems[j].actionId;
                                returnPage.pageListSet.listItems[i].listSubItems[j].action = ConvertActionObject(page.lists.list[i].listItems[j].action);
                            }
                        }
                        returnPage.pageListSet.listItems[i].actionID = page.lists.list[i].actionId;
                        returnPage.pageListSet.listItems[i].action = ConvertActionObject(page.lists.list[i].action);
                    }
                }
                #endregion

            }
            if (page.pageInfo != null)
            {
                #region 解析翻页信息
                returnPage.pageListInfo = new WSOA_PageListInfo();
                if (page.pageInfo.count != null)
                {
                    returnPage.pageListInfo.pageNo = page.pageInfo.count.index;
                    returnPage.pageListInfo.totalPageCount = page.pageInfo.count.all;
                }
                if (page.pageInfo.first != null)
                {
                    returnPage.pageListInfo.firstPageUrl = new WSOA_PageListInfoUrl();
                    returnPage.pageListInfo.firstPageUrl.img = page.pageInfo.first.img;
                    returnPage.pageListInfo.firstPageUrl.message = page.pageInfo.first.message;
                    returnPage.pageListInfo.firstPageUrl.actionID = page.pageInfo.first.actionId;
                    returnPage.pageListInfo.firstPageUrl.action = ConvertActionObject(page.pageInfo.first.action);
                }
                if (page.pageInfo.last != null)
                {
                    returnPage.pageListInfo.lastPageUrl = new WSOA_PageListInfoUrl();
                    returnPage.pageListInfo.lastPageUrl.img = page.pageInfo.last.img;
                    returnPage.pageListInfo.lastPageUrl.message = page.pageInfo.last.message;
                    returnPage.pageListInfo.lastPageUrl.actionID = page.pageInfo.last.actionId;
                    returnPage.pageListInfo.lastPageUrl.action = ConvertActionObject(page.pageInfo.last.action);
                }
                if (page.pageInfo.prev != null)
                {
                    returnPage.pageListInfo.prevPageUrl = new WSOA_PageListInfoUrl();
                    returnPage.pageListInfo.prevPageUrl.img = page.pageInfo.prev.img;
                    returnPage.pageListInfo.prevPageUrl.message = page.pageInfo.prev.message;
                    returnPage.pageListInfo.prevPageUrl.actionID = page.pageInfo.prev.actionId;
                    returnPage.pageListInfo.prevPageUrl.action = ConvertActionObject(page.pageInfo.prev.action);
                }
                if (page.pageInfo.next != null)
                {
                    returnPage.pageListInfo.nextPageUrl = new WSOA_PageListInfoUrl();
                    returnPage.pageListInfo.nextPageUrl.img = page.pageInfo.next.img;
                    returnPage.pageListInfo.nextPageUrl.message = page.pageInfo.next.message;
                    returnPage.pageListInfo.nextPageUrl.actionID = page.pageInfo.next.actionId;
                    returnPage.pageListInfo.nextPageUrl.action = ConvertActionObject(page.pageInfo.next.action);
                }
                #endregion
            }
            if (page.fields != null)
            {
                List<WSOA_PageField> fieldList = new List<WSOA_PageField>();
                if (!string.IsNullOrEmpty(page.fields.fieldUrl))
                {
                    HttpResponse response = HttpService.HttpRequest(HTTPROOTURL + page.fields.fieldUrl.Replace("amp;", ""), HTTPPOSTMETHOD, null, Encoding.UTF8, cookies);
                    if (response.exeFlag)
                    {
                        fieldList.AddRange(ParseHtmlField(response.returnStr));
                    }
                }
                if (page.fields.fields != null && page.fields.fields.Length > 0)
                {
                    #region 解析表单信息
                    for (int i = 0; i < page.fields.fields.Length; i++)
                    {
                        WSOA_PageField pageField = new WSOA_PageField();
                        pageField.fieldID = page.fields.fields[i].fieldId;
                        pageField.fieldType = page.fields.fields[i].type;
                        pageField.fieldTitle = page.fields.fields[i].title;
                        pageField.fieldName = page.fields.fields[i].name;
                        pageField.fieldValue = page.fields.fields[i].value;
                        pageField.required = page.fields.fields[i].require;
                        pageField.separator = page.fields.fields[i].separator;
                        pageField.reset = page.fields.fields[i].reset;
                        pageField.fieldHide = page.fields.fields[i].hide;
                        pageField.fieldDisable = page.fields.fields[i].dable;
                        pageField.fieldRead = page.fields.fields[i].read;
                        pageField.rowCount = page.fields.fields[i].rows;

                        if (page.fields.fields[i].btn != null && page.fields.fields[i].btn.Length > 0)
                        {
                            if (page.fields.fields[i].type.Equals("button"))
                            {
                                for (int j = 0; j < page.fields.fields[i].btn.Length; j++)
                                {
                                    if (page.fields.fields[i].btn[j].isDisabled != null && page.fields.fields[i].btn[j].isDisabled.Equals("1"))
                                    {
                                        continue;
                                    }
                                    WSOA_PageMenu pageMenu = new WSOA_PageMenu();
                                    pageMenu.message = page.fields.fields[i].btn[j].message;
                                    pageMenu.img = page.fields.fields[i].btn[j].img;
                                    pageMenu.actionID = page.fields.fields[i].btn[j].actionId;
                                    pageMenu.action = ConvertActionObject(page.fields.fields[i].btn[j].action);

                                    menus.Add(pageMenu);
                                }
                            }
                            else
                            {
                                pageField.fieldButtons = new WSOA_PageFieldButton[page.fields.fields[i].btn.Length];
                                for (int j = 0; j < page.fields.fields[i].btn.Length; j++)
                                {
                                    pageField.fieldButtons[j] = new WSOA_PageFieldButton();
                                    pageField.fieldButtons[j].actionID = page.fields.fields[i].btn[j].actionId;
                                    pageField.fieldButtons[j].img = page.fields.fields[i].btn[j].img;
                                    pageField.fieldButtons[j].message = page.fields.fields[i].btn[j].message;
                                    pageField.fieldButtons[j].action = ConvertActionObject(page.fields.fields[i].btn[j].action);
                                }
                            }
                        }

                        if (page.fields.fields[i].option != null && page.fields.fields[i].option.Length > 0)
                        {
                            pageField.fieldOptions = new WSOA_PageFieldOption[page.fields.fields[i].option.Length];
                            for (int j = 0; j < page.fields.fields[i].option.Length; j++)
                            {
                                pageField.fieldOptions[j] = new WSOA_PageFieldOption();
                                pageField.fieldOptions[j].optionID = page.fields.fields[i].option[j].optionId;
                                pageField.fieldOptions[j].message = page.fields.fields[i].option[j].message;
                                pageField.fieldOptions[j].optionValue = page.fields.fields[i].option[j].value;
                                pageField.fieldOptions[j].optionHide = page.fields.fields[i].option[j].hide;
                                pageField.fieldOptions[j].optionDisable = page.fields.fields[i].option[j].dable;
                                pageField.fieldOptions[j].optionRead = page.fields.fields[i].option[j].optionId;
                                if (page.fields.fields[i].option[j].saction != null)
                                {
                                    pageField.fieldOptions[j].sactionShow = page.fields.fields[i].option[j].saction.show;
                                    pageField.fieldOptions[j].sactionHide = page.fields.fields[i].option[j].saction.hide;
                                    pageField.fieldOptions[j].sactionAble = page.fields.fields[i].option[j].saction.able;
                                    pageField.fieldOptions[j].sactionDisable = page.fields.fields[i].option[j].saction.dable;
                                }
                                if (page.fields.fields[i].option[j].saction != null)
                                {
                                    pageField.fieldOptions[j].usactionShow = page.fields.fields[i].option[j].usaction.show;
                                    pageField.fieldOptions[j].usactionHide = page.fields.fields[i].option[j].usaction.hide;
                                    pageField.fieldOptions[j].usactionAble = page.fields.fields[i].option[j].usaction.able;
                                    pageField.fieldOptions[j].usactionDisable = page.fields.fields[i].option[j].usaction.dable;
                                }
                            }
                        }
                        fieldList.Add(pageField);
                    }
                    if (fieldList.Count > 0)
                    {
                        returnPage.pageFields = fieldList.ToArray();
                    }
                    #endregion
                }
            }
            if (page.actions != null && page.actions.Length > 0)
            {
                returnPage.actions = new WSOA_Action[page.actions.Length];
                for (int i = 0; i < page.actions.Length; i++)
                {
                    returnPage.actions[i] = ConvertActionObject(page.actions[i]);
                }
            }

            returnPage.pageMenus = menus.ToArray();
            
            return returnPage;
        }

        /// <summary>
        /// 转换动作对象
        /// </summary>
        public static WSOA_Action ConvertActionObject(HTTPOA_Action action)
        {
            if (action == null)
            {
                return null;
            }
            WSOA_Action returnAction = new WSOA_Action();
            returnAction.actionID = action.actionId;

            if (action.pageConfirm != null)
            {
                returnAction.pageConfirm = new WSOA_PageConfirm();
                returnAction.pageConfirm.img = action.pageConfirm.img;
                returnAction.pageConfirm.message = action.pageConfirm.message;
            }
            if (action.pagePrompt != null)
            {
                action.pagePrompt = new HTTPOA_PagePrompt();
                action.pagePrompt.img = action.pagePrompt.img;
                action.pagePrompt.message = action.pagePrompt.message;
            }

            returnAction.changeNotedite = action.notedited;
            returnAction.url = action.url;
            returnAction.goBackUrl = action.goback;
            returnAction.funcType = action.fun;
            if (action.progress != null)
            {
                returnAction.exeProgress = action.progress.message;
            }
            if (action.title != null)
            {
                returnAction.pageTitle = new WSOA_PageTitle();
                returnAction.pageTitle.img = action.title.img;
                returnAction.pageTitle.message = action.title.message;
                returnAction.pageTitle.desc = action.title.desc;
            }
            returnAction.listfield = action.listfield;
            if (action.argument != null && action.argument.Length > 0)
            {
                returnAction.arguements = new WSOA_PostField[action.argument.Length];
                for (int i = 0; i < action.argument.Length; i++)
                {
                    returnAction.arguements[i] = new WSOA_PostField();
                    returnAction.arguements[i].fieldName = action.argument[i].name;
                    returnAction.arguements[i].fieldValues = action.argument[i].values;
                }
            }
            returnAction.pname = action.pname;
            returnAction.phname = action.phname;
            returnAction.pvalue = action.pvalue;
            if (action.selectDataSet != null)
            {
                returnAction.actionDataSet = new WSOA_ActionDataSet();
                returnAction.actionDataSet.dataSetType = action.selectDataSet.type;
                if (action.selectDataSet.datas != null && action.selectDataSet.datas.Length > 0)
                {
                    returnAction.actionDataSet.actionDatas = new WSOA_ActionData[action.selectDataSet.datas.Length];
                    for (int i = 0; i < action.selectDataSet.datas.Length; i++)
                    {
                        returnAction.actionDataSet.actionDatas[i] = new WSOA_ActionData();
                        returnAction.actionDataSet.actionDatas[i].img = action.selectDataSet.datas[i].img;
                        returnAction.actionDataSet.actionDatas[i].message = action.selectDataSet.datas[i].message;
                        returnAction.actionDataSet.actionDatas[i].value = action.selectDataSet.datas[i].value;
                        returnAction.actionDataSet.actionDatas[i].hiddenValue = action.selectDataSet.datas[i].hvalue;
                    }
                }
            }
            returnAction.nextAction = action.next;

            return returnAction;
        }

        /// <summary>
        /// 转换组织结构
        /// </summary>
        public static WSOA_Dept[] ConvertDeptObject(HTTPOA_Dept[] depts)
        {
            if (depts == null || depts.Length == 0)
            {
                return null;
            }
            WSOA_Dept[] returnDepts = new WSOA_Dept[depts.Length];
            for (int i = 0; i < depts.Length; i++)
            {
                if (depts[i] == null)
                {
                    continue;
                }
                WSOA_Dept returnDept = new WSOA_Dept();
                HTTPOA_Dept dept = depts[i];

                returnDept.deptName = dept.deptName;
                if (dept.roles != null && dept.roles.Length > 0)
                {
                    WSOA_Role[] roles = new WSOA_Role[dept.roles.Length];
                    for (int j = 0; j < dept.roles.Length; j++)
                    {
                        if (dept.roles[j] == null)
                        {
                            continue;
                        }
                        WSOA_Role returnRole = new WSOA_Role();
                        HTTPOA_Role role = dept.roles[j];

                        returnRole.roleName = role.roleName;
                        if (role.users != null && role.users.Length > 0)
                        {
                            WSOA_User[] users = new WSOA_User[role.users.Length];
                            for (int k = 0; k < role.users.Length; k++)
                            {
                                if (role.users[k] == null)
                                {
                                    continue;
                                }
                                WSOA_User returnUser = new WSOA_User();
                                HTTPOA_User user = role.users[k];

                                returnUser.userName = user.userName;
                                returnUser.sex = user.sex;

                                users[k] = returnUser;
                            }
                            returnRole.users = users;
                        }

                        roles[j] = returnRole;
                    }
                    returnDept.roles = roles;
                }
                if (dept.depts != null && dept.depts.Length > 0)
                {
                    returnDept.depts = ConvertDeptObject(dept.depts);
                }
                returnDepts[i] = returnDept;
            }

            return returnDepts;
        }

        public static List<WSOA_PageField> ParseHtmlField(string htmlStr)
        {
            List<WSOA_PageField> fields = new List<WSOA_PageField>();
            Parser parser = Parser.CreateParser(htmlStr, "UTF8");
            NodeList htmlNodes = parser.ExtractAllNodesThatMatch(new TagNameFilter("td"));

            for (int i = 0; i < htmlNodes.Count; i++)
            {
                if (htmlNodes[i] is ITag)
                {
                    ITag tag = (htmlNodes[i] as ITag);
                    if (tag.Attributes != null && tag.Attributes.Count > 0)
                    {
                        if (tag.Attributes["CLASS"] != null && (tag.Attributes["CLASS"].ToString().Equals("LabelText") || tag.Attributes["CLASS"].ToString().Equals("TableTitle")))
                        {
                            //TD中不含NOTTOAPP标签
                            if (tag.Attributes["NOTTOAPP"] != null && tag.Attributes["NOTTOAPP"].ToString().Equals("1"))
                            {
                                continue;
                            }
                            ITag parentTag = (tag.Parent.Parent as ITag);
                            //表格中不含NOTTOAPP标签
                            if (parentTag.Attributes["NOTTOAPP"] != null && parentTag.Attributes["NOTTOAPP"].ToString().Equals("1"))
                            {
                                continue;
                            }
                            WSOA_PageField field = new WSOA_PageField();
                            SetFieldTitle(field, htmlNodes[i].FirstChild);
                            //如果是标题头的话，下一个字段不包含textarea
                            if (tag.Attributes["CLASS"].ToString().Equals("LabelText") || (tag.Attributes["CLASS"].ToString().Equals("TableTitle") && htmlNodes[i + 1].ToHtml().IndexOf("textarea") > -1))
                            {
                                if (htmlNodes[i + 1].Children == null || htmlNodes[i + 1].Children.Count == 0)
                                {
                                    field.fieldType = "text";
                                }
                                else
                                {
                                    SetFieldContent(field, htmlNodes[i + 1]);
                                }
                            }
                            else
                            {
                                field.fieldType = "text";
                            }
                            //Console.WriteLine(htmlNodes[i].ToHtml());
                            //Console.WriteLine(htmlNodes[i + 1].ToHtml());
                            //Console.WriteLine("-----------------------------");
                            if (field != null)
                            {
                                fields.Add(field);
                            }
                        }
                    }
                }
            }
            return fields;
        }

        public static String escape(String src)
        {
            int i;
            char j;
            StringBuilder tmp = new StringBuilder();
            tmp.EnsureCapacity(src.Length * 6);

            char[] chs = src.ToCharArray();
            for (i = 0; i < chs.Length; i++)
            {
                j = chs[i];


                if (Char.IsDigit(j) || Char.IsLower(j) || Char.IsUpper(j))
                    tmp.Append(j);
                else if (j < 256)
                {
                    tmp.Append("%");
                    if (j < 16)
                        tmp.Append("0");
                    tmp.Append(Convert.ToString((int)j, 16));
                }
                else
                {
                    tmp.Append("%u");
                    tmp.Append(Convert.ToString((int)j, 16));
                }
            }
            return tmp.ToString();
        }

        public static bool isMenuScreened(string message)
        {
            if (string.IsNullOrEmpty(message)) return true;
            string[] screenStrings = new string[] { "返回", "退回", "返回首页", "取消", "退出", "退回首页" };
            foreach (string screenStr in screenStrings)
            {
                if (screenStr.Equals(message))
                    return true;
            }
            return false;
        }

        public static void SetFieldTitle(WSOA_PageField field, INode htmlNode)
        {
            INode parseNode = htmlNode;
            while (parseNode.ToHtml().Replace("\n", "").Length == 0)
            {
                parseNode = htmlNode.NextSibling;
            }
            if (parseNode is ITag)
            {
                ITag tag = (parseNode as ITag);
                if (tag.TagName.Equals("INPUT"))
                {
                    field.fieldTitle = tag.Attributes["VALUE"].ToString();

                    //field.separator = ";";
                    //field.fieldRead = "1";
                    //field.fieldButtons = new WSOA_PageFieldButton[1];

                    //WSOA_PageFieldButton btn = new WSOA_PageFieldButton();
                    //btn.message = "相关人员";
                    //btn.action = new WSOA_Action();
                    //btn.action.pageTitle = new WSOA_PageTitle();
                    //btn.action.pageTitle.message = "选择相关人员";
                    //btn.action.funcType = "h";
                    //btn.action.pname = "DUsers";
                    //btn.action.actionDataSet = new WSOA_ActionDataSet();
                    //btn.action.actionDataSet.dataSetType = "unit";

                    //field.fieldButtons[0] = btn;
                }
            }
            else
            {
                field.fieldTitle = parseNode.GetText();
            }
        }

        public static void SetFieldContent(WSOA_PageField field, INode htmlNode)
        {
            for (int i = 0; i < htmlNode.Children.Count; i++)
            {
                INode childNode = htmlNode.Children[i];
                if (childNode is ITag)
                {
                    ITag tag = (childNode as ITag);
                    if (tag.TagName.Equals("INPUT"))
                    {
                        //if (field.fieldButtons != null)
                        //{
                        //    field.fieldType = "input";
                        //    if (tag.Attributes["VALUE"] != null)
                        //    {
                        //        field.fieldValue = tag.Attributes["VALUE"].ToString().Replace("; ", ";");
                        //    }
                        //    if (tag.Attributes["NAME"] != null)
                        //    {
                        //        field.fieldName = tag.Attributes["NAME"].ToString();
                        //        field.fieldButtons[0].action.pname = field.fieldName;
                        //    }
                        //    return;
                        //}
                        if (tag.Attributes["TYPE"] == null)
                        {
                            if (tag.Attributes["APPTYPE"] == null)
                            {
                                field.fieldType = "input";
                                if (tag.Attributes["VALUE"] != null)
                                {
                                    field.fieldValue = tag.Attributes["VALUE"].ToString();
                                }
                                if (tag.Attributes["NAME"] != null)
                                {
                                    field.fieldName = tag.Attributes["NAME"].ToString();
                                }
                                return;
                            }
                            else if (tag.Attributes["APPTYPE"].ToString().Equals("unit"))
                            {
                                field.fieldType = "input";

                                field.separator = ";";
                                field.fieldRead = "1";
                                field.fieldButtons = new WSOA_PageFieldButton[1];

                                WSOA_PageFieldButton btn = new WSOA_PageFieldButton();
                                btn.message = "相关人员";
                                btn.action = new WSOA_Action();
                                btn.action.pageTitle = new WSOA_PageTitle();
                                btn.action.pageTitle.message = "选择相关人员";
                                btn.action.funcType = "h";
                                btn.action.pname = "DUsers";
                                btn.action.actionDataSet = new WSOA_ActionDataSet();
                                btn.action.actionDataSet.dataSetType = "unit";

                                field.fieldButtons[0] = btn;

                                if (tag.Attributes["VALUE"] != null)
                                {
                                    field.fieldValue = tag.Attributes["VALUE"].ToString().Replace("; ", ";");
                                }
                                if (tag.Attributes["NAME"] != null)
                                {
                                    field.fieldName = tag.Attributes["NAME"].ToString();
                                    field.fieldButtons[0].action.pname = field.fieldName;
                                }

                                return;
                            }
                            else if (tag.Attributes["APPTYPE"].ToString().Equals("date"))
                            {
                                field.fieldType = "date";
                                if (tag.Attributes["VALUE"] != null)
                                {
                                    field.fieldValue = tag.Attributes["VALUE"].ToString();
                                }
                                if (tag.Attributes["NAME"] != null)
                                {
                                    field.fieldName = tag.Attributes["NAME"].ToString();
                                }
                                return;
                            }
                            else if (tag.Attributes["APPTYPE"].ToString().Equals("num"))
                            {
                                field.fieldType = "input";
                                field.required = "number";
                                if (tag.Attributes["VALUE"] != null)
                                {
                                    field.fieldValue = tag.Attributes["VALUE"].ToString();
                                }
                                if (tag.Attributes["NAME"] != null)
                                {
                                    field.fieldName = tag.Attributes["NAME"].ToString();
                                }
                                return;
                            }
                            else if (tag.Attributes["APPTYPE"].ToString().Equals("time"))
                            {
                                field.fieldType = "time";
                                if (tag.Attributes["VALUE"] != null)
                                {
                                    field.fieldValue = tag.Attributes["VALUE"].ToString();
                                }
                                if (tag.Attributes["NAME"] != null)
                                {
                                    field.fieldName = tag.Attributes["NAME"].ToString();
                                }
                                return;
                            }
                            else if (tag.Attributes["APPTYPE"].ToString().Equals("datetime"))
                            {
                                field.fieldType = "datetime";
                                if (tag.Attributes["VALUE"] != null)
                                {
                                    field.fieldValue = tag.Attributes["VALUE"].ToString();
                                }
                                if (tag.Attributes["NAME"] != null)
                                {
                                    field.fieldName = tag.Attributes["NAME"].ToString();
                                }
                                return;
                            }
                        }
                        else if (tag.Attributes["TYPE"].ToString().Equals("radio"))
                        {
                            field.fieldType = "radio";
                            ParseRadio(field, htmlNode);
                            return;
                        }
                        else if (tag.Attributes["TYPE"].ToString().Equals("checkbox"))
                        {
                            field.fieldType = "checkbox";
                            ParseCheckBox(field, htmlNode);
                            return;
                        }
                    }
                    else if (tag.TagName.Equals("LABEL"))
                    {
                        if (tag.ToHtml().IndexOf("type=\"radio\"") > -1)
                        {
                            field.fieldType = "radio";
                            ParseRadio(field, htmlNode);
                            return;
                        }
                        else if (tag.ToHtml().IndexOf("type=\"checkbox\"") > -1)
                        {
                            field.fieldType = "checkbox";
                            ParseCheckBox(field, htmlNode);
                            return;
                        }
                    }
                    else if (tag.TagName.Equals("SELECT"))
                    {
                        field.fieldType = "combobox";
                        if (tag.Attributes["NAME"] != null)
                        {
                            field.fieldName = tag.Attributes["NAME"].ToString();
                        }
                        ParseSelect(field, childNode);
                        return;
                    }
                    else if (tag.TagName.Equals("TEXTAREA"))
                    {
                        //if (field.fieldButtons != null)
                        //{
                        //    field.fieldType = "textarea";
                        //    if (tag.FirstChild != null)
                        //    {
                        //        field.fieldValue = tag.FirstChild.ToHtml().Replace("; ", ";");
                        //    }
                        //    if (tag.Attributes["NAME"] != null)
                        //    {
                        //        field.fieldName = tag.Attributes["NAME"].ToString();
                        //    }
                        //    field.separator = ";";
                        //    field.fieldRead = "1";
                        //    field.fieldButtons = new WSOA_PageFieldButton[1];

                        //    WSOA_PageFieldButton btn = new WSOA_PageFieldButton();
                        //    btn.message = "相关人员";
                        //    btn.action = new WSOA_Action();
                        //    btn.action.pageTitle = new WSOA_PageTitle();
                        //    btn.action.pageTitle.message = "选择相关人员";
                        //    btn.action.funcType = "h";
                        //    btn.action.pname = field.fieldName;
                        //    btn.action.actionDataSet = new WSOA_ActionDataSet();
                        //    btn.action.actionDataSet.dataSetType = "unit";

                        //    field.fieldButtons[0] = btn;
                        //    return;
                        //}

                        field.fieldType = "textarea";
                        if (tag.FirstChild != null)
                        {
                            field.fieldValue = tag.FirstChild.ToHtml().Replace("; ", ";");
                        }
                        if (tag.Attributes["NAME"] != null)
                        {
                            field.fieldName = tag.Attributes["NAME"].ToString();
                        }
                        if (tag.Attributes["APPTYPE"] != null && tag.Attributes["APPTYPE"].ToString().Equals("unit"))
                        {
                            field.separator = ";";
                            field.fieldRead = "1";
                            field.fieldButtons = new WSOA_PageFieldButton[1];

                            WSOA_PageFieldButton btn = new WSOA_PageFieldButton();
                            btn.message = "相关人员";
                            btn.action = new WSOA_Action();
                            btn.action.pageTitle = new WSOA_PageTitle();
                            btn.action.pageTitle.message = "选择相关人员";
                            btn.action.funcType = "h";
                            btn.action.pname = "DUsers";
                            btn.action.actionDataSet = new WSOA_ActionDataSet();
                            btn.action.actionDataSet.dataSetType = "unit";

                            field.fieldButtons[0] = btn;
                        }
                        return;
                    }
                    else if (tag.TagName.Equals("A"))
                    {
                        field.fieldType = "text";
                        if (childNode.FirstChild != null)
                        {
                            field.fieldValue = childNode.FirstChild.GetText();
                        }
                        return;
                    }
                }
                else if (!string.IsNullOrEmpty(childNode.GetText().Replace("\n", "")))
                {
                    field.fieldType = "text";
                    field.fieldValue = childNode.GetText();
                    return;
                }
            }
        }

        public static void ParseRadio(WSOA_PageField field, INode htmlNode)
        {
            List<WSOA_PageFieldOption> options = new List<WSOA_PageFieldOption>();
            for (int i = 0; i < htmlNode.Children.Count; i++)
            {
                INode childNode = htmlNode.Children[i];
                if (childNode is ITag)
                {
                    ITag tag = (childNode as ITag);
                    if (tag.TagName.Equals("INPUT") && tag.Attributes["TYPE"] != null && tag.Attributes["TYPE"].ToString().Equals("radio"))
                    {
                        WSOA_PageFieldOption option = new WSOA_PageFieldOption();
                        option.message = childNode.NextSibling.GetText().Replace("\n", "");
                        option.optionValue = tag.Attributes["VALUE"].ToString();
                        if (childNode.ToHtml().IndexOf("checked") > -1)
                        {
                            field.fieldValue = tag.Attributes["VALUE"].ToString();
                        }
                        options.Add(option);
                    }
                    else if (tag.TagName.Equals("LABEL"))
                    {
                        for (int j = 0; j < tag.Children.Count; j++)
                        {
                            if (tag.Children[j] is ITag)
                            {
                                ITag childTag = (tag.Children[j] as ITag);
                                if (childTag.TagName.Equals("INPUT") && childTag.Attributes["TYPE"] != null && childTag.Attributes["TYPE"].ToString().Equals("radio"))
                                {
                                    WSOA_PageFieldOption option = new WSOA_PageFieldOption();
                                    option.message = childTag.NextSibling.GetText().Replace("\n", "");
                                    option.optionValue = childTag.Attributes["VALUE"].ToString();
                                    if (childTag.ToHtml().IndexOf("checked") > -1)
                                    {
                                        field.fieldValue = childTag.Attributes["VALUE"].ToString();
                                    }
                                    options.Add(option);
                                }
                            }
                        }
                    }
                }
            }
            field.fieldOptions = options.ToArray();
        }

        public static void ParseSelect(WSOA_PageField field, INode htmlNode)
        {
            List<WSOA_PageFieldOption> options = new List<WSOA_PageFieldOption>();
            for (int i = 0; i < htmlNode.Children.Count; i++)
            {
                INode childNode = htmlNode.Children[i];
                if (childNode is ITag)
                {
                    ITag tag = (childNode as ITag);
                    if (tag.TagName.Equals("OPTION"))
                    {
                        WSOA_PageFieldOption option = new WSOA_PageFieldOption();
                        option.message = childNode.FirstChild.GetText().Replace("\n", "");
                        option.optionValue = tag.Attributes["VALUE"].ToString().Replace("\n", "");
                        if (childNode.ToHtml().IndexOf("selected") > -1)
                        {
                            field.fieldValue = tag.Attributes["VALUE"].ToString().Replace("\n", "");
                        }
                        options.Add(option);
                    }
                }
            }
            field.fieldOptions = options.ToArray();
        }

        public static void ParseCheckBox(WSOA_PageField field, INode htmlNode)
        {
            List<WSOA_PageFieldOption> options = new List<WSOA_PageFieldOption>();
            for (int i = 0; i < htmlNode.Children.Count; i++)
            {
                INode childNode = htmlNode.Children[i];
                if (childNode is ITag)
                {
                    ITag tag = (childNode as ITag);
                    if (tag.TagName.Equals("INPUT") && tag.Attributes["TYPE"] != null && tag.Attributes["TYPE"].ToString().Equals("checkbox"))
                    {
                        WSOA_PageFieldOption option = new WSOA_PageFieldOption();
                        option.message = childNode.NextSibling.GetText().Replace("\n", "");
                        option.optionValue = tag.Attributes["VALUE"].ToString();
                        if (childNode.ToHtml().IndexOf("checked") > -1)
                        {
                            field.fieldValue = field.fieldValue + tag.Attributes["VALUE"].ToString() + ";";
                        }
                        options.Add(option);
                    }
                    else if (tag.TagName.Equals("LABEL"))
                    {
                        for (int j = 0; j < tag.Children.Count; j++)
                        {
                            if (tag.Children[j] is ITag)
                            {
                                ITag childTag = (tag.Children[j] as ITag);
                                if (childTag.TagName.Equals("INPUT") && childTag.Attributes["TYPE"] != null && childTag.Attributes["TYPE"].ToString().Equals("checkbox"))
                                {
                                    WSOA_PageFieldOption option = new WSOA_PageFieldOption();
                                    option.message = childTag.NextSibling.GetText().Replace("\n", "");
                                    option.optionValue = childTag.Attributes["VALUE"].ToString();
                                    if (childTag.ToHtml().IndexOf("checked") > -1)
                                    {
                                        field.fieldValue = field.fieldValue + childTag.Attributes["VALUE"].ToString() + ";";
                                    }
                                    options.Add(option);
                                }
                            }
                        }
                    }
                }
            }
            field.separator = ";";
            field.fieldValue = field.fieldValue.Substring(0, field.fieldValue.Length - 1);
            field.fieldOptions = options.ToArray();
        }
    }
}
